package it.stasbranger.myproject.service;

import com.google.gwt.requestfactory.shared.Locator;
import com.google.gwt.requestfactory.shared.ServiceLocator;

import it.stasbranger.myproject.domain.Requests;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

/**
 * Implements GWT Request Factory Locator for both Entities and Service, acts as an adapter between the GWT RequestFactory
 * contract and the JPA/Spring back end.
 * This class is designed to enable integration of Entities that may have composite primary keys and that may not have a version field etc.
 * It is also designed to push requests from GWT through a standard DAO and Service layer
 */
@Component
public class RequestsGWTAdapter extends Locator<Requests, String> implements
		ServiceLocator, ApplicationContextAware {

	@Override
	public Requests create(Class<? extends Requests> clazz) {
		return new Requests();
	}

	@Override
	public Requests find(Class<? extends Requests> clazz, String id) {
		Object[] keyValues = getPrimaryKeysFromGoogleKey(id);
		Integer Idrequests = (Integer) keyValues[0];

		return getRequestsService().findRequestsByPrimaryKey(Idrequests);
	}

	@Override
	public Class<Requests> getDomainType() {
		return Requests.class;
	}

	@Override
	public String getId(Requests domainObject) {
		return createGoogleKey(domainObject);
	}

	@Override
	public Class<String> getIdType() {
		return String.class;
	}

	@Override
	public Object getVersion(Requests requests) {
		// If your entity has a version field, return it here, the scaffolding engine did not detect a version field for this entity
		// we have generated code to return a version that is based on the contents of the fields of the Entity, GWT uses the version number to detect changes to Entity Proxies
		final int prime = 31;
		int result = 1;
		result = prime * result + ((requests.getIdrequests() == null) ? 0 : requests.getIdrequests().hashCode());
		result = prime * result + ((requests.getDate() == null) ? 0 : requests.getDate().hashCode());
		result = prime * result + ((requests.getName() == null) ? 0 : requests.getName().hashCode());
		result = prime * result + ((requests.getEmail() == null) ? 0 : requests.getEmail().hashCode());
		return result;
	}

	public Object getInstance(Class<?> clazz) {
		if (it.stasbranger.myproject.service.RequestsService.class.equals(clazz))
			return getRequestsService();

		return null;
	}

	/**
	 * Returns the Primary Key field values in the order they are required by the DAO layer to find by primary key
	 * @param key
	 * @return
	 */
	protected Object[] getPrimaryKeysFromGoogleKey(String key) {
		String[] keyValuesAsText = new String[] { key };

		Object[] keyValues = new Object[keyValuesAsText.length];
		keyValues[0] = new Integer(keyValuesAsText[0]);

		return keyValues;
	}

	/**
	 * Returns a single String that can uniquely identify an instance to the GWT Client
	 * The primary key values are encoded in the order they are required by the DAO layer to findByPrimaryKey for convenience
	 * @param object
	 * @return
	 */
	protected String createGoogleKey(Requests requests) {
		StringBuilder key = new StringBuilder();
		key.append(requests.getIdrequests());
		return key.toString();
	}

	/**
	 * 
	 */
	public RequestsService getRequestsService() {
		RequestsService service;

		service = (RequestsService) context.getBean("RequestsService");

		return service;
	}

	// Spring Context Management
	private static ApplicationContext context;

	/**
	 * The Spring Context will be pushed in when the container is initialized
	 */
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}
}
